#include "stdafx.h"
#include "World.h"
#include <algorithm>

CWorld::CWorld():
m_currentDwarf(-1)
{
    m_conditions.push_back("day");
    m_conditions.push_back("night");
    m_commands.push_back("right");
    m_commands.push_back("left");
    m_commands.push_back("forward");
    m_commands.push_back("backward");
    m_commands.push_back("say");
    m_commands.push_back("go");
    m_commands.push_back("stop");
    m_commands.push_back("attack");
    m_commands.push_back("work");
}


CWorld::~CWorld()
{
}

void CWorld::SelectDwarf(int id)
{
    if (id < m_dwarves.size())
    {
        m_currentDwarf = id;
        cout << endl << "WORLD: Dwarf #" << id << " was successfully selected" << endl;
    }
    else
    {
        cout << endl << "WORLD: ERROR: Invalid dwarf id." << endl;
    }
}

void CWorld::CreateDwarf(DwarfType type)
{
    CDwarf dwarf(type);
    DwarfPtr pDwarf(new CDwarf(type));
    m_dwarves.push_back(pDwarf);
    cout << endl << "WORLD: Dwarf #" << m_dwarves.size() - 1 << " was successfully added." << endl;
}

void CWorld::Command(string word)
{
    if ((m_currentDwarf > m_dwarves.size()) || (m_currentDwarf < 0))
    {
        cout << endl << "WORLD: ERROR: Dwarf was not selected." << endl;
    }
    else
    {
        cout << endl << "WORLD: Dwarf #" << m_currentDwarf << " (" << m_dwarves[m_currentDwarf]->GetTypeStr() << ") " << word << "." << endl;
    }
}

bool CWorld::IsValidDwarfId(int id)
{
    return (id >= 0) && (id < m_dwarves.size());
}

bool CWorld::IsValidCondition(string str)
{
    return std::find(m_conditions.begin(), m_conditions.end(), str) != m_conditions.end();
}

bool CWorld::IsValidCommand(string str)
{
    return std::find(m_commands.begin(), m_commands.end(), str) != m_commands.end();
}

void CWorld::While(string arg, string command)
{
    if ((m_currentDwarf > m_dwarves.size()) || (m_currentDwarf < 0))
    {
        cout << endl << "WORLD: ERROR: Dwarf was not selected." << endl;
    }
    else
    {
        cout << endl
            << "WORLD: Dwarf #" << m_currentDwarf <<
            " (" << m_dwarves[m_currentDwarf]->GetTypeStr() << ") "
            << command << " while " << arg << "." << endl;
    }
}